package com.ruoyi.radar.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.radar.common.CommonUtil;
import com.ruoyi.radar.domain.RadarSilo;
import com.ruoyi.radar.domain.RadarSiloResult;
import com.ruoyi.radar.mapper.RadarSiloMapper;
import com.ruoyi.radar.service.RadarSiloResultService;
import com.ruoyi.radar.mapper.RadarSiloResultMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

/**
* @author BD
* @description 针对表【radar_silo_result】的数据库操作Service实现
* @createDate 2024-12-05 18:07:41
*/
@Service
public class RadarSiloResultServiceImpl extends ServiceImpl<RadarSiloResultMapper, RadarSiloResult>
    implements RadarSiloResultService{

    @Autowired
    private RadarSiloResultMapper radarSiloResultMapper;

    @Autowired
    private RadarSiloResultService radarSiloResultService;

    @Autowired
    CommonUtil commonUtil;

    @Autowired
    protected Validator validator;

    @Override
    public List<RadarSiloResult> selectSiloResult(Map<String, Object> param) {
        if(!"1".equals(param.get("type"))){
            commonUtil.toPage(param);
        }
        List<RadarSiloResult> radarSiloResults = radarSiloResultMapper.selectSiloResult(param);
        return radarSiloResults;
    }

    @Override
    public Integer getCount(Map<String, Object> param) {
        return radarSiloResultMapper.getCount(param);
    }

    @Override
    public String importSilo(List<RadarSiloResult> siloList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(siloList) || siloList.size() == 0)
        {
            throw new ServiceException("导入料仓数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (RadarSiloResult radarSiloResult : siloList)
        {
            try
            {
                // 验证是否存在这个点位
                HashMap<String, Object> map = new HashMap<>();
                map.put("siloStockId",radarSiloResult.getResultId());

                List<RadarSiloResult> radarSiloResults = radarSiloResultMapper.selectSiloResult(map);
//                if (StringUtils.isNull(gnssPoint))
                if (radarSiloResults.size() == 0)
                {
                    BeanValidators.validateWithException(validator, user);
//                    radarSilo.setDcsEquipId(commonUtil.uuId());
                    radarSiloResult.setCreateTime(commonUtil.currentTime());

                    radarSiloResultService.save(radarSiloResult);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、编号 " + radarSiloResult.getResultId() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, user);
//                    radarSilo.setSiloStockId(commonUtil.checkNull(GnssRecieveEquips.get(0).getDcsEquipId()));
//                    radarSilo.se(commonUtil.currentTime());
                    radarSiloResultService.updateById(radarSiloResult);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、编号 " + radarSiloResult.getResultId() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、编号 " + radarSiloResult.getResultId() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、编号 " + radarSiloResult.getResultId() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}




